<template>
  <el-dialog v-model="dialog.visible" title="分配对接人" width="45%" @close="handleCloseDialog">
    <div class="form-container">
      <el-card shadow="never" class="table-card">
        <div class="table-card-body">
          <BasePageTable
            :table-data="tableData"
            :table-columns-data="tableColumnsData"
            :total="total"
            :loading="loading"
          >
            <template #operation="{ row, name }">
              <el-button link type="primary" icon="Edit" @click="handleAssign(row)">分配</el-button>
            </template>
          </BasePageTable>
        </div>
      </el-card>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCloseDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ElMessage } from "element-plus";
import HouseAPI, { ConsumerHouseConstructionLinkForm, HousePageVO } from "@/api/house";
import { useRequest } from "@/hooks/useRequest";

import BasePageTable from "@/components/BasePageTable/index.vue";
import opportunityPaymentAPI, { ImUserAllotQuery } from "@/api/opportunityPayment";
// 定义 emit，用于通知外部关闭事件
const emit = defineEmits(["update:modelValue", "loadData"]);
const dialog = reactive({
  title: "",
  visible: false,
});
/*基础信息*/
const formRef = ref();
const currentRow = ref({});

const queryParams = reactive<ImUserAllotQuery>({
  name: "",
});
const loading = ref(false);
const total = ref(0);
const tableData = ref<HousePageVO[]>([]);
const tableColumnsData = ref([
  {
    prop: "nickname",
    label: "用户昵称",
    showOverflowTooltip: true,
  },
  {
    prop: "mobile",
    label: "电话",
    showOverflowTooltip: true,
  },
  {
    prop: "roleName",
    label: "角色",
    showOverflowTooltip: true,
  },

  {
    prop: "operation",
    label: "操作",
    width: 150,
    align: "center",
    customSlot: "operation", // 自定义插槽名称
  },
]);
const defaultFormData: ConsumerHouseConstructionLinkForm = {
  companyId: undefined,
  contractAmount: undefined,
  contractTime: undefined,
  contractFile: [],
  createBy: undefined,
  createTime: "",
  decorationAddress: "",
  decorationCompanyName: "",
  designerName: "",
  designerPhone: "",
  houseId: undefined,
  id: "",
  merchantUserId: undefined,
  paymentFile: [],
  receiptFile: [],
  status: undefined,
  updateBy: 0,
  updateTime: "",
};
const formData = ref({
  ...defaultFormData,
});

// 关闭弹窗
function handleCloseDialog() {
  dialog.visible = false;
  Object.assign(formData.value, defaultFormData);
  emit("update:modelValue", false); // 关闭弹窗
}

function showModal(val) {
  dialog.visible = true;
  currentRow.value = val;
  Object.assign(formData.value, defaultFormData);
  handleQuery();
}

async function handleQuery() {
  try {
    loading.value = true;
    const res = await opportunityPaymentAPI.getAllotChatUsers({});
    tableData.value = res.data;
    loading.value = false;
  } catch (err) {
    console.log(err);
  } finally {
    loading.value = false;
  }
}

async function handleAssign(val) {
  const result = await ElMessageBox.confirm(`确认将对接人分配给${val.nickname}吗?`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  });
  if (!result) return;
  assignChatUser(val);
}

async function assignChatUser(val) {
  try {
    await opportunityPaymentAPI.setChatUser({
      id: currentRow.value?.id,
      chatUserId: val.id,
    });
    ElMessage.success("分配成功");
    handleCloseDialog();
    emit("loadData");
  } catch (e) {
    console.log(e);
  }
}

defineExpose({
  showModal,
});
</script>
<style style="scss" scoped>
.form-container {
  height: 600px;
  overflow: auto;
}
</style>
